using System.Collections.Generic;
using System.Web.Routing;
using Nop.Core.Plugins;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;

namespace Nop.Plugin.Widgets.SpreadButton
{
    /// <summary>
    /// Live person provider
    /// </summary>
    public class SpreadButtonPlugin : BasePlugin, IWidgetPlugin
    {
        private readonly ISettingService _settingService;
        private readonly SpreadButtonSettings _spreadButtonSettings;

        public SpreadButtonPlugin(ISettingService settingService,
            SpreadButtonSettings spreadButtonSettings)
        {
            this._settingService = settingService;
            this._spreadButtonSettings = spreadButtonSettings;
        }

        /// <summary>
        /// Gets widget zones where this widget should be rendered
        /// </summary>
        /// <returns>Widget zones</returns>
        public IList<string> GetWidgetZones()
        {
            return !string.IsNullOrWhiteSpace(_spreadButtonSettings.WidgetZone)
                       ? new List<string>() { _spreadButtonSettings.WidgetZone }
                       : new List<string>() { "body_end_html_tag_before" };
        }

        /// <summary>
        /// Gets a route for provider configuration
        /// </summary>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "Configure";
            controllerName = "WidgetSpreadButton";
            routeValues = new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Widgets.SpreadButton.Controllers" }, { "area", null } };
        }

        /// <summary>
        /// Gets a route for displaying widget
        /// </summary>
        /// <param name="widgetZone">Widget zone where it's displayed</param>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetDisplayWidgetRoute(string widgetZone, out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "PublicInfo";
            controllerName = "WidgetSpreadButton";
            routeValues = new RouteValueDictionary()
            {
                {"Namespaces", "Nop.Plugin.Widgets.SpreadButton.Controllers"},
                {"area", null},
                {"widgetZone", widgetZone}
            };
        }

        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {
            var settings = new SpreadButtonSettings()
            {
                PublicApiKey = "1111122222333334444455555666667777788888999990000011111222",
                PrivateApiKey = "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5",
                TrackingScript = @"<!-- Tracking code for SpreadButton -->
<script type=""text/javascript"">
var publicKeySb = ""{PUBLICAPIKEY}"" ;
var sbsetcookie = true ;
var iproto = ((""https:"" == document.location.protocol) ? ""https://"" : ""http://"");
document.write(unescape(""%3Cscript src='"" + iproto + ""static-sb.com/js/sb-tracker.js' type='text/javascript'%3E%3C/script%3E""));
</script>
",


            };
            _settingService.SaveSetting(settings);

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.SpreadButton.PublicApiKey", "Public API Key");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.SpreadButton.PublicApiKey.Hint", "Enter SpreadButton public API Key.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.SpreadButton.PrivateApiKey", "Private API Key");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.SpreadButton.PrivateApiKey.Hint", "Enter SpreadButton private API Key.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.SpreadButton.TrackingScript", "Tracking code");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.SpreadButton.TrackingScript.Hint", "Paste the tracking code here. {PUBLICAPIKEY}, {REGISTER} and {ECOMMERCE} will be dynamically replaced.");
            
            base.Install();
        }

        /// <summary>
        /// Uninstall plugin
        /// </summary>
        public override void Uninstall()
        {
            //settings
            _settingService.DeleteSetting<SpreadButtonSettings>();

            //locales
            this.DeletePluginLocaleResource("Plugins.Widgets.SpreadButton.PublicApiKey");
            this.DeletePluginLocaleResource("Plugins.Widgets.SpreadButton.PublicApiKey.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.SpreadButton.PrivateApiKey");
            this.DeletePluginLocaleResource("Plugins.Widgets.SpreadButton.PrivateApiKey.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.SpreadButton.TrackingScript");
            this.DeletePluginLocaleResource("Plugins.Widgets.SpreadButton.TrackingScript.Hint");
            
            base.Uninstall();
        }
    }
}
